package com.yuerlive.rainbow.module.homepage.presenter;

import com.yuerlive.rainbow.api.SubscribeApi;
import com.yuerlive.rainbow.api.SubscribeService;
import com.yuerlive.rainbow.api.UserApi;
import com.yuerlive.rainbow.api.UserService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Admin on 2017/7/4.
 */

public class HomePagePresenter extends BasePresenterImpl implements HomePageContract.Presenter {
    private HomePageContract.View view;
    private UserService userService;
    private SubscribeService subscribeService;
    public HomePagePresenter(HomePageContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        userService = UserApi.getInstance().service;
        subscribeService = SubscribeApi.getInstance().service;

    }
    @Override
    public void getHomePageData(String otherId) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        params.put("otherId", otherId);
        userService.getUserInfo(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<UserInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<UserInfo> anchorBaseData) {
                        view.hideLoading();
                        view.showResult(anchorBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void subscribe(String anchorId) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        params.put("upUserId", anchorId);
        subscribeService.subscribe(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showSubscribeResult(baseData.getCode(), anchorId);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }
}
